/**
 * 
 */
package edu.gatech.oad.yuno;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.Toast;

/**
 * @author michael
 *
 */
public class MapActivity extends com.google.android.maps.MapActivity {
	private MapView map;
	List<Overlay> mapOverlays;
	Drawable drawable;
	MapItemizedOverlay itemizedOverlay;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
		map = (MapView)findViewById(R.id.map);
		map.setBuiltInZoomControls(true);
	}
	
	public void onResume() {
		super.onResume();
		SharedPreferences prefs = getSharedPreferences("YUNoDos", MODE_PRIVATE);
		
		LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		String lp = lm.getBestProvider(new Criteria(), true);
		
		android.location.Location l = lm.getLastKnownLocation(lp);
		if (l != null) {
			GeoPoint p = new GeoPoint((int)(l.getLatitude()*1E6), (int)(l.getLongitude()*1E6));
			map.getController().setCenter(p);
			map.getController().setZoom(15);
		}
		
		mapOverlays = map.getOverlays();
		drawable = this.getResources().getDrawable(R.drawable.challengeaccepted);
		itemizedOverlay = new MapItemizedOverlay(drawable);
		
		for (Location loc : Location.findAllByUser(prefs.getLong("logged_in_user_id", 0))) {
			if (loc.numberOfTodos() < 1) continue; //only show locations with tasks
			GeoPoint point = new GeoPoint((int)(loc.getLatitude()*1E6), (int)(loc.getLongitude()*1E6));
			OverlayItem overlayitem = new OverlayItem(point,loc.getName(),Integer.toString(loc.numberOfTodos()));
			itemizedOverlay.addOverlay(overlayitem);	
		}
		
		mapOverlays.add(itemizedOverlay);
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	
}
